﻿using System;
using System.ComponentModel.DataAnnotations.Schema;
using Volo.Abp.Domain.Entities;

namespace Ebusiness_UserService.Model
{
    //[Table("T_UserAccessFail")]
    public class UserAccessFail : Entity<Guid>
    {
        //public Guid ld { get; init; }
        public Guid? Userld { get; private set; }//用户Id

        public User User { get; init; }//用户

        //public PhoneNumber Phone { get; private set; }

        private bool lockOut;//是否锁定
        public DateTime? LockoutEnd { get; private set; }
        public int AccessFailedCount { get; private set; }
        private UserAccessFail()
        {

        }

        public UserAccessFail(bool setlockOut,int setAccessFailedCount)
        {
            this.lockOut = setlockOut;
            this.AccessFailedCount = setAccessFailedCount;
        }

        public void Reset()
        {
            lockOut = false;
            LockoutEnd = null;
            AccessFailedCount = 0;
        }

        public void Fail()
        {
            //处理一次“登录失败”
            AccessFailedCount++;
            if (AccessFailedCount >= 3)
            {
                //可以添加领域事件或集成事件高速账户被锁定了，保存修改成功之后发布事件
                lockOut = true;
                LockoutEnd = DateTime.Now.AddMinutes(5);
            }
        }

        public bool lsLockOut()
        {
            //是否已经锁定
            if (lockOut)
            {
                if (LockoutEnd >= DateTime.Now)
                {
                    return true;
                }
                else //锁定已经到期
                {
                    AccessFailedCount = 0;
                    LockoutEnd = null;
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}